package com.tkjt.pms.modules.tkindexvaluerule.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tkjt.pms.modules.tkindexvaluerule.entity.TkIndexValueRule;
import com.tkjt.pms.modules.tkindexvaluerule.entity.TkIndexValueRuleLog;
import com.tkjt.pms.modules.tkindexvaluerule.mapper.TkIndexValueRuleLogMapper;
import com.tkjt.pms.modules.tkindexvaluerule.mapper.TkIndexValueRuleMapper;
import com.tkjt.pms.modules.tkindexvaluerule.service.ITkIndexValueRuleService;
import com.tkjt.pms.modules.tkindexvaluerule.vo.TkIndexValueRuleVO;
import org.apache.commons.beanutils.BeanUtils;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: tk_index_value_rule
 * @Author: jeecg-boot
 * @Date:   2020-02-17
 * @Version: V1.0
 */
@Service
public class TkIndexValueRuleServiceImpl extends ServiceImpl<TkIndexValueRuleMapper, TkIndexValueRule> implements ITkIndexValueRuleService {

    @Autowired
    TkIndexValueRuleMapper tkIndexValueRuleMapper;
    @Autowired
    TkIndexValueRuleLogMapper tkIndexValueRuleLogMapper;
    @Override
    public Result<?> list(TkIndexValueRuleVO tkIndexValueRuleVO) {
        int pageNo = tkIndexValueRuleVO.getPageNo();
        int pageSize = tkIndexValueRuleVO.getPageSize();
        Map<String, Object> queryIndexValue = new HashMap<>();
        queryIndexValue.put("min", (pageNo - 1) * pageSize);
        queryIndexValue.put("max", pageSize);
        List<TkIndexValueRuleVO> list = tkIndexValueRuleMapper.selectPage(queryIndexValue);
        int count = tkIndexValueRuleMapper.selectCount(queryIndexValue);
        Page<TkIndexValueRuleVO> finalPage = new Page<TkIndexValueRuleVO>(pageNo,pageSize);
        finalPage.setTotal(count);
        finalPage.setCurrent(pageNo);
        finalPage.setSize(pageSize);
        int pages = count / pageSize;
        if (count % pageSize > 0) {
            pages += 1;
        }
        finalPage.setPages(pages);
        finalPage.setRecords(list);
        return Result.ok(finalPage);
    }

    @Override
    public Result<?> edit(TkIndexValueRule tkIndexValueRule) {
        TkIndexValueRule oldRule = tkIndexValueRuleMapper.selectById(tkIndexValueRule.getId());
        if(oldRule != null){
            oldRule.setId(null);
            oldRule.setUpdateDate(new Date());
            TkIndexValueRuleLog tkIndexValueRuleLog = new TkIndexValueRuleLog();
            try {
                BeanUtils.copyProperties(tkIndexValueRuleLog, oldRule);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            tkIndexValueRuleLogMapper.insert(tkIndexValueRuleLog);
        }
        tkIndexValueRuleMapper.updateById(tkIndexValueRule);
        return Result.ok("编辑成功!");
    }
}
